﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.CRM.Bussiness;
using System.Data;
using ZX.CRM.Report.CrmReport;
using ZX.CRM.Report.CrmReport.InnerTrain;

namespace ZX.WebApp.Crm.Report
{
    public partial class CrmInnerTrain : System.Web.UI.Page
    {
        BCrmReport CrmReport = new BCrmReport();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtStime.Value = DateTime.Now.Year.ToString() + "-01-01";
                txtEtime.Value = DateTime.Now.ToShortDateString();
            }
            if (txtStime.Value != "")
            {
                if (txtEtime.Value == "")
                {
                    txtEtime.Value = DateTime.Now.ToString();
                }
                DoReport(int.Parse(rblList.SelectedValue), txtStime.Value, txtEtime.Value);
            }
            else
            {
                DoReport(int.Parse(rblList.SelectedValue), txtStime.Value, txtEtime.Value);
            }
        }
        private void DoReport(int type, string sDate, string eDate)
        {
            try
            {
                getCss();
                DataSet myDataSet = new DataSet();
                myDataSet.DataSetName = "DsInnerTrain";
                DataTable DtInnerTrain = CrmReport.ReturnTableInnerTeach(type, Convert.ToDateTime(sDate), DateTime.Parse(eDate));
                DataTable DtInner1 = CrmReport.ReturnSumInnerTeach(type, Convert.ToDateTime(sDate), DateTime.Parse(eDate));
                DataTable DtInner2 = CrmReport.ReturnSumInnerTeach(type, Convert.ToDateTime(sDate).AddMonths(-1), DateTime.Parse(eDate).AddMonths(-1));
                DtInnerTrain.TableName = "DtInnerTrain";
                DtInner1.TableName = "DtInner1";
                DtInner2.TableName = "DtInner2";
                DataTable datetable = new DataTable("DtParm");
                myDataSet.Tables.Add(datetable);
                datetable.Columns.Add("dateParm", typeof(string));
                datetable.Columns.Add("TypeName", typeof(string));
                string strYear_Month = Convert.ToDateTime(sDate).Year + "年" + Convert.ToDateTime(sDate).Month + "月-" + Convert.ToDateTime(eDate).Year + "年" + Convert.ToDateTime(eDate).Month + "月";
                if (Convert.ToDateTime(sDate).Year == Convert.ToDateTime(eDate).Year)
                {
                    strYear_Month = Convert.ToDateTime(sDate).Year + "年" + Convert.ToDateTime(sDate).Month + "-" + Convert.ToDateTime(eDate).Month + "月";
                    if (Convert.ToDateTime(sDate).Month == Convert.ToDateTime(eDate).Month)
                    {
                        strYear_Month = Convert.ToDateTime(sDate).Year + "年" + Convert.ToDateTime(sDate).Month + "月";
                    }
                }

                datetable.Rows.Add(new object[] { strYear_Month, rblList.SelectedItem.Text });
                myDataSet.Tables.Add(DtInnerTrain.Copy());
                myDataSet.Tables.Add(DtInner1.Copy());
                myDataSet.Tables.Add(DtInner2.Copy());
                RptDeliverTrain crm = new RptDeliverTrain();
                crm.DataSource = myDataSet;
                RV.Report = crm;
            }
            catch
            {
            }


            //}
        }
        private void getStartEndDate()
        {
            if (hdnFd.Value == "month")
            {
                txtStime.Value = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); txtEtime.Value = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
            }
            else if (hdnFd.Value == "year")
            {
                txtStime.Value = DateTime.Now.Year.ToString() + "-01-01";
                txtEtime.Value = DateTime.Now.Year.ToString() + "-12-31";
            }
            else if (hdnFd.Value == "season")
            {
                int season = DateTime.Now.Month % 3;
                if (season == 0)
                {
                    txtStime.Value = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 2).ToString() + "-01";
                    txtEtime.Value = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
                else if (season == 1)
                {
                    txtStime.Value = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); txtEtime.Value = DateTime.Now.AddMonths(3).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
                else if (season == 2)
                {
                    txtStime.Value = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-01";
                    txtEtime.Value = DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
            }
        }
        private void getCss()
        {
            lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
            lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "blue");
            lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
            if (txtStime.Value == DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString() && txtEtime.Value == DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString())
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "red");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "blue");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
            }
            else if (txtStime.Value == DateTime.Now.Year.ToString() + "-01-01" &&
                txtEtime.Value == DateTime.Now.Year.ToString() + "-12-31")
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "blue");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "red");
            }
            else
            {
                int season = DateTime.Now.Month % 3;
                if (season == 0)
                {
                    if (txtStime.Value == DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 2).ToString() + "-01" && txtEtime.Value == DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString())
                    {
                        lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                        lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                        lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
                else if (season == 1)
                {
                    if (txtStime.Value == DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString() && txtEtime.Value == DateTime.Now.AddMonths(3).AddDays(-DateTime.Now.Day).ToShortDateString())
                    {
                        lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                        lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                        lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
                else if (season == 2)
                {
                    if (txtStime.Value == DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-01" && txtEtime.Value == DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day).ToShortDateString())
                    {
                        lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                        lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                        lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
            }
        }
        /// <summary>
        /// 查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            getStartEndDate();
            DoReport(int.Parse(rblList.SelectedValue), txtStime.Value, txtEtime.Value);
        }
    }
}